///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	ExportAll.m
//	SUMMARY:	Implementation of a multi-format SaveAll
//	SUPERCLASS:	Object
//	INTERFACE:	None
//	PROTOCOLS:	<Tool>
//	AUTHOR:		Rohit Khare
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//	DESCRIPTION: 
//	Designed to save all open documents in the HTMLD format.
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	10/31/94:	Created. Implemented in just minutes, from scratch.
///////////////////////////////////////////////////////////////////////////////

#import "ExportAll.h"

@implementation ExportAll

+ new 
{
	static ExportAll *ea = nil;
	
	if (!ea) {
		ea = [[ExportAll alloc] init];
	}
	return ea;
}

+ toolAwake:theApp
{
	[theApp registerAccessory:NXUniqueString("Export ALL Documents")
				key:'\0'
			   name:NXUniqueString("ExportAll")
			 target:[ExportAll new]
			 action:@selector(exportAll:)];
	[theApp registerAccessory:NXUniqueString("Export Open Documents")
				key:'\0'
			   name:NXUniqueString("ExportOpen")
			 target:[ExportAll new]
			 action:@selector(exportOpen:)];
	[theApp registerAccessory:NXUniqueString("Print Open Documents")
				key:'\0'
			   name:NXUniqueString("PrintAll")
			 target:[ExportAll new]
			 action:@selector(printAll:)];
	return self;
}

- init {return [super init];}

- free {return self;}

- exportOpen:sender
{
	char	path[MAXPATHLEN];
	char	newPath[MAXPATHLEN];
	const char 	*docdir;
	long	key; 
	id		doc; 
	NXHashState  state;
	id docTable;
	
	int choice, fmt;
	
	choice = NXRunAlertPanel("Export All","What format would you like to export all open, untouched (current .etfd on disk) documents to?", "HTML","LaTeX","ASCII");
	switch (choice) {
		case NX_ALERTDEFAULT: fmt = HTMD_FMT; break;
		case NX_ALERTALTERNATE: fmt = TeXD_FMT; break;
		case NX_ALERTOTHER: fmt = ASCII_FMT; break;
	}

	
	docTable= [[NXApp etApp] docTable];
	state = [docTable initState];
	
	while ([docTable nextState: &state key:(void **)&key value:(void **)&doc] &&
			!NXUserAborted())  {
		if (![doc needsSaving]) {
			strcpy(path, [[doc docInfo] docPath]);
			// CRITICALLY IMPORTANT: come up with a virgin path for each format
			switch (fmt) {
				case HTMD_FMT:  *rindex(path, '.') = 0;
								docdir = [[NXApp userModel] stringQuery:HTMDIRECTORY];
								if (docdir && *docdir) {
									sprintf(newPath,"%s/%s."HTMD_EXT, 
											docdir, rindex(path, '/')+1);
								} else {
									sprintf(newPath,"%s."HTMD_EXT, path);
								}
								break;
				case TeXD_FMT:  *rindex(path, '.') = 0;
								sprintf(newPath,"%s."TeXD_EXT, path); 
								break;
				case ASCII_FMT: *rindex(path, '.') = 0;
								sprintf(newPath,"%s."ASCII_EXT, path); 
								break;
				default: 		return nil;
			}
			if (*newPath && strcmp(newPath, path)) {
				[doc saveTo:newPath inFormat:fmt changePath:NO];
			}
		}
	}
	return self;
}

- exportAll:sender
{
	char	path[MAXPATHLEN];
	char	newPath[MAXPATHLEN];
	const char 	*docdir;
	id		docInfo; 
	int  i,N;
	id docList;
	
	int choice, fmt;
	
	choice = NXRunAlertPanel("Export All","What format would you like to export EVERY DOCUMENT to?", "HTML","LaTeX","ASCII");
	switch (choice) {
		case NX_ALERTDEFAULT: fmt = HTMD_FMT; break;
		case NX_ALERTALTERNATE: fmt = TeXD_FMT; break;
		case NX_ALERTOTHER: fmt = ASCII_FMT; break;
	}

	
	docList= [[navigator query:"*" field: "*"] copy];
	N = [docList count];
	
	for (i=0; i<N; i++)  {
		if (NXUserAborted()) break;
		docInfo = [docList objectAt:(i)];
		[etApp openID:[docInfo docID]];
		NXPing(); // Paranoia
		if ([docInfo etDoc] && ![[docInfo etDoc] needsSaving]) {
			[[[[docInfo etDoc] docUI] window] display];
			strcpy(path, [docInfo docPath]);
			// CRITICALLY IMPORTANT: come up with a virgin path for each format
			switch (fmt) {
				case HTMD_FMT:  *rindex(path, '.') = 0;
								docdir = [[NXApp userModel] stringQuery:HTMDIRECTORY];
								if (docdir && *docdir) {
									sprintf(newPath,"%s/%s."HTMD_EXT, 
											docdir, rindex(path, '/')+1);
								} else {
									sprintf(newPath,"%s."HTMD_EXT, path);
								}
								break;
				case TeXD_FMT:  *rindex(path, '.') = 0;
								sprintf(newPath,"%s."TeXD_EXT, path); 
								break;
				case ASCII_FMT: *rindex(path, '.') = 0;
								sprintf(newPath,"%s."ASCII_EXT, path); 
								break;
				default: 		return nil;
			}
			if (*newPath && strcmp(newPath, path)) {
				[[docInfo etDoc] saveTo:newPath inFormat:fmt changePath:NO];
			}
		}
		[etApp closeID:[docInfo docID]];
	}
	[docList free];
	return self;
}

static BOOL runPrintPanel=YES;

-(BOOL)shouldRunPrintPanel:aView {return runPrintPanel;} 
// batch-mode printing requests... you were warned!
- printAll:sender {
	long    key; 
	id 		doc; 
	NXHashState  state;
	id docTable = [[NXApp etApp] docTable];
	state = [docTable initState];
	
	runPrintPanel= YES;
	while ([docTable nextState: &state key:(void **)&key value:(void **)&doc] &&
			!NXUserAborted())  {
		[[doc docUI] print:self];
		// run the panel only once
		if (runPrintPanel) runPrintPanel = NO;
	}
	runPrintPanel = YES;
	return self;
}
@end